﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using RkLibs.Security;
using RkLibs.Functions;
namespace RkLibs.Configuration
{
    /// <summary>
    /// 配置操作类
    /// </summary>
    public static class Cfg
    {
        /// <summary>
        /// 配置静态类
        /// </summary>
        private static IConfiguration config = null;
        /// <summary>
        /// 配置文件路径
        /// </summary>
        private static string ConfigFilePath = null;
        /// <summary>
        /// 配置文件临时文件
        /// </summary>
        private static string ConfigTempPath = null;
        /// <summary>
        /// 配置文件类型
        /// </summary>
        private static ConfigFileType ConfigFileType = ConfigFileType.INI;

        /// <summary>
        /// 初始化配置类
        /// </summary>
        /// <param name="fileType">配置文件类型</param>
        /// <param name="filePath">配置文件路径</param>
        public static void Initialize(ConfigFileType fileType, string filePath)
        {
            ConfigFilePath = filePath;
            CfgFileInit();
        }

        /// <summary>
        /// 初始化配置类
        /// 使用默认配置
        /// </summary>
        public static void Initialize()
        {
            ConfigFilePath = AppDomain.CurrentDomain.BaseDirectory + "cfg.bin";
            CfgFileInit();
        }
        /// <summary>
        /// 初始化配置文件路径
        /// </summary>
        private static void CfgFileInit()
        {
            ConfigTempPath = Path.GetTempFileName();
            if (File.Exists(ConfigFilePath))
            {
                byte[] src = File.ReadAllBytes(ConfigFilePath);
                string str = DES.DecryptBeytsToString(src);
                File.WriteAllText(ConfigTempPath, str);
            }
            config = ConfigFactory.GetConfig(ConfigFileType, ConfigTempPath);
        }

        /// <summary>
        /// 写入配置
        /// </summary>
        /// <param name="section">域</param>
        /// <param name="key">关键字</param>
        /// <param name="value">值</param>
        /// <returns>true写入成功，false写入失败</returns>
        public static bool WriteConfig(string section, string key, object value)
        {
            return config.WriteConfig(section,  key,  value);
        }

        /// <summary>
        /// 读取bool值
        /// </summary>
        /// <param name="section">域</param>
        /// <param name="key">关键字</param>
        /// <param name="def">默认值</param>
        /// <returns>读取的值</returns>
        public static bool ReadBool(string section, string key, bool def = true)
        {
            return config.ReadBool(section, key, def);
        }

        /// <summary>
        /// 读取整型值
        /// </summary>
        /// <param name="section">域</param>
        /// <param name="key">关键字</param>
        /// <param name="def">默认值</param>
        /// <returns>读取的值</returns>
        public static int ReadInt(string section, string key, int def = 0)
        {
            return config.ReadInt(section, key, def);
        }

        /// <summary>
        /// 读取字符串值
        /// </summary>
        /// <param name="section">域</param>
        /// <param name="key">关键字</param>
        /// <param name="def">默认值</param>
        /// <returns>读取的值</returns>
        public static string ReadString(string section, string key, string def = "")
        {
            return config.ReadString(section, key, def);
        }

        /// <summary>
        /// 删除域
        /// </summary>
        /// <param name="section">域</param>
        /// <returns>true删除成功，false删除失败</returns>
        public static bool DeleteSection(string section)
        {
            return config.DeleteSection(section);
        }

        /// <summary>
        /// 删除字段
        /// </summary>
        /// <param name="section">域</param>
        /// <param name="key">字段</param>
        /// <returns>true删除成功，false删除失败</returns>
        public static bool DeleteKey(string section, string key)
        {
            return config.DeleteKey(section, key);
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        public static void Refresh()
        {
            if (File.Exists(ConfigTempPath))
            {
                string str = File.ReadAllText(ConfigTempPath);
                byte[] src = DES.EncryptStringToBytes(str);
                File.WriteAllBytes(ConfigFilePath, src);
            }
        }
        public static void Close()
        {
            Refresh();
            CommonFunction.DeleteFile(ConfigTempPath);
        }
    }
}
